package com.gk.controller;

import com.gk.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
//使用拦截器，完成后台系统登录验证，登录用户名为姓名，密码为学号。
@Controller
public class UserController {
    @RequestMapping("/main")
    public String toMainPage() {
        return "main";
    }
    @RequestMapping("/toLogin")
        public String toLoginPage() {
        return "login";
    }
    @RequestMapping("/orderInfo")
    public String orderInfo() {
        return "orderinfo";
    }

    /**
     * 用户登录
     */
    @RequestMapping("/login")
    public String login(User user, Model model, HttpSession session) {
        String userName = user.getUsername();
        String password = user.getPassword();

        if((userName != null && userName.equals("周金鑫")) && (password != null && password.equals("2022131495"))) {
            session.setAttribute("USER_SESSION",user);
            return "main";
        }
        model.addAttribute("msg","用户名或密码错误，请重新登录");
        return "login";
    }
    /**
     * 用户退出
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:toLogin";
    }
}
